package com.oriental.manage.service.settlement.Impl;

import com.alibaba.dubbo.common.json.JSONObject;
import com.google.gson.reflect.TypeToken;
import com.oriental.check.commons.util.BeanMapperUtil;
import com.oriental.manage.core.paging.Pagination;
import com.oriental.manage.core.response.ResponseDTO;
import com.oriental.manage.core.utils.*;
import com.oriental.manage.pojo.merchant.settleManage.MchntPayableamtManage;
import com.oriental.manage.service.settlement.MchntPayableAmtService;
import com.oriental.opcif.common.util.DateUtil;
import com.oriental.reserve.api.trans.DayTransDetailInterface;
import com.oriental.reserve.model.ResponseModel;
import com.oriental.reserve.model.business.DayTransDetailDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author: yutao
 * Date: 2017/5/23
 * Time: 15:47
 * Desc：
 */
@Service
@Slf4j
public class MchntPayableAmtManServiceImpl implements MchntPayableAmtService{

    @Autowired
    private DayTransDetailInterface dayTransDetailInterface;
    @Value("#{cfgProperties['bill_httpUrl']}")
    private  String bill_httpUrl;

    @Override
    public void query(ResponseDTO<Pagination<MchntPayableamtManage>> responseDTO, Pagination<MchntPayableamtManage> pagination) {
        DayTransDetailDto dayTransDetailDto = BeanMapperUtil.objConvert(pagination.getQueryBean(), DayTransDetailDto.class);
        ResponseModel<List<DayTransDetailDto>> responseModel = dayTransDetailInterface.selectModelPage(dayTransDetailDto);
        List<DayTransDetailDto> dayTransDetailDtos = responseModel.getResult();
        List<MchntPayableamtManage> mchntPayableamtManages = BeanMapperUtil.mapList(dayTransDetailDtos, MchntPayableamtManage.class);
        if(mchntPayableamtManages !=null && mchntPayableamtManages.size()>0){
                pagination.setRowCount(mchntPayableamtManages.get(0).getRowCount());
                for(MchntPayableamtManage mchntPayableamtManage:mchntPayableamtManages){

                    if(mchntPayableamtManage.getBankTransPayDate() !=null || mchntPayableamtManage.getBankTransRecDate() !=null){
                        mchntPayableamtManage.setClrStatus("1");
                    }else {
                        mchntPayableamtManage.setClrStatus("0");
                    }

                    if(mchntPayableamtManage.getOrgCode().indexOf("_") == -1){
                        mchntPayableamtManage.setType("1");

                    }else{
                        mchntPayableamtManage.setType("0");
                        int i = mchntPayableamtManage.getOrgCode().indexOf("_");
                        mchntPayableamtManage.setOrgCode(mchntPayableamtManage.getOrgCode().substring(0,i));

                    }

                    if(mchntPayableamtManage.getBankTransPayDate() == null && mchntPayableamtManage.getBankTransRecDate() != null) {
                        if(dayTransDetailDto.getBankTransPayDate() == null){
                            mchntPayableamtManage.setBankTransPayDate(mchntPayableamtManage.getBankTransRecDate());
                        }else if (mchntPayableamtManage.getBankTransRecDate().compareTo(dayTransDetailDto.getBankTransPayDate()) > 0) {
                            mchntPayableamtManage.setBankTransPayDate(mchntPayableamtManage.getBankTransRecDate());
                        }
                    }
                    try{
                        Map<String,String> httpParms = new HashMap<>();
                        httpParms.put("version","V1.0");
                        httpParms.put("reqSerNum", DateUtil.getCurrent().concat(RandomMath.getNum(5)));
                        httpParms.put("reqTime", DateUtils.getToDay(DateUtils.DATETIMESFORMAT));
                        httpParms.put("mchntCode",mchntPayableamtManage.getOrgCode());
                        String settleDate = DateUtils.format(mchntPayableamtManage.getTransDate(), DateUtils.DATESHORTFORMAT);
                        httpParms.put("settleDate",settleDate);
                        httpParms.put("spare1","");
                        String parms = GsonUtil.getInstance().toJson(httpParms);
                        log.info("http查询商户交易确认日期信息请求URL:{}",bill_httpUrl);
                        log.info("http查询商户交易确认日期信息请求参数:{}",parms);
                        BillHttpClient billHttpClient = new BillHttpClient(bill_httpUrl);
                        String resultData = billHttpClient.post(parms);
                        log.info("http查询商户交易确认日期信息响应信息:{}",resultData);

                        Map<String, String> respMap= GsonUtil.getInstance().fromJson(resultData, new TypeToken<Map<String, String>>() {}.getType());
                        if(StringUtils.equals((String)respMap.get("respCode"),"0000") && StringUtils.equals((String)respMap.get("confirmStatus"),"B")){
                            String confirmTime = String.valueOf(respMap.get("confirmTime"));
                            Date date = new Date(Long.valueOf(confirmTime));
                            mchntPayableamtManage.setConfirmTime(date);
                        }


                    }catch (Exception e){
                        log.error("http查询商户交易确认日期异常:{}",e);
//                        throw new RuntimeException("http查询商户交易确认日期异常"+e);
                    }





                }

        }else{
            pagination.setRowCount(0);
        }
        responseDTO.setSuccess(true);
        pagination.setList(mchntPayableamtManages);
        responseDTO.setSumObject(responseModel.getErrorMsg());
        responseDTO.setObject(pagination);

    }


}
